package com.magic.gre.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.entity.WordsBean;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;
import com.noname.lib_base_java.util.SelectHelper;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-07 18:06
 * 文件名: WordsAdapter
 * 描述:
 */
public class WordsAdapter extends CommonAdapter<WordsBean> {

    //生词本
    public static final int NEW_WORDS = 9999;
    //熟词本
    public static final int RIPE_WORDS = 9998;

    private int tag;

    private OnWordsListener onWordsListener;

    public void setOnWordsListener(OnWordsListener onWordsListener) {
        this.onWordsListener = onWordsListener;
    }

    public WordsAdapter(Context mContext, List<WordsBean> mData, int tag) {
        super(mContext, mData);
        this.tag = tag;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(viewType, parent, false));
    }

    /**
     * @param holder
     * @param position
     */
    @Override
    protected void bindData(ViewHolder holder, final int position) {
        if (getItemViewType(position) == R.layout.item_words_ripe) {
            final WordsBean bean = mData.get(position - 1);
            holder.getView(R.id.select_iv).setSelected(bean.isSelect());
            holder.setText(R.id.words_tv, bean.getName());
            holder.setText(R.id.content_tv, bean.getMeaning());
            holder.getView(R.id.select_iv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                    bean.setSelect(v.isSelected());
                    if (SelectHelper.isSelectAll(mData)) {
                        if (onWordsListener != null) {
                            onWordsListener.onSelectAll();
                        }
                    } else if (SelectHelper.isNotSelectAll(mData)) {
                        if (onWordsListener != null) {
                            onWordsListener.onNotSelectAll();
                        }
                    }

                    notifyDataSetChanged();
                }
            });
        } else if (getItemViewType(position) == R.layout.item_words) {
            WordsBean bean = mData.get(position);
            holder.setText(R.id.words_tv, bean.getName());
            holder.setText(R.id.content_tv, bean.getMeaning());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onWordsListener != null) {
                        onWordsListener.onItemClickListener(position);
                    }
                }
            });


        } else if (getItemViewType(position) == R.layout.item_words_ripe_head) {
            List<WordsBean> selectBeans = SelectHelper.getSelectBeans(mData);
            String strNum = String.valueOf(selectBeans.size());
            String strAll = String.valueOf(mData.size());
            String content = "已选  " + strNum + " / " + strAll + "  词";

            int start = content.indexOf(strNum);
            int end = start + strNum.length();

            SpannableString spannableString = new SpannableString(content);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.main_color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            TextView textView = holder.getView(R.id.select_tv);
            textView.setText(spannableString, TextView.BufferType.SPANNABLE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (tag == NEW_WORDS) {
            return R.layout.item_words;
        } else {
            if (position == 0) {
                return R.layout.item_words_ripe_head;
            } else {
                return R.layout.item_words_ripe;
            }

        }
    }

    @Override
    public int getItemCount() {
        if (tag == NEW_WORDS) {
            return super.getItemCount();
        } else {
            return super.getItemCount() + 1;
        }
    }

    public interface OnWordsListener {
        void onSelectAll();

        void onNotSelectAll();

        void onItemClickListener(int position);
    }
}
